function WsPowerNet(ws,powerNet) {
	this.ws = ws
	this.powerNet = powerNet
	this.listeners = {}
	this.nodes = {} //id:{ui:node ,data:{}}
	this.edges = {}// id:{ui:node ,data:{}}

	this.mode = 0 //0: normal mode , 1: line mode

	this.uuid = ""+Math.random()
	
	this.init()
}


WsPowerNet.prototype.init = function() {
	Q.Defaults.LABEL_EDITOR_SUBMIT_WHEN_LOST_FOCUS = true;
	this.initWs()
	this.initGraph()
}
WsPowerNet.prototype.initGraph = function() {
	var self = this
	var graph = this.powerNet.graph

	// Q.createToolbar(graph)
	// this.graph.onclick = function(evt) {
	// 	self.sendGraphJson(evt)
	// }
	graph.enddrag = function(evt) {
		console.log("enddrag",evt)
		self.onGraphEndDrag(evt)
		
	}
	// this.powerNet.graph.onrelease = function(evt) {
	// 	self.onGraphRelease(evt)
	// }
	var gm  = graph.graphModel
	// 元素属性变化触发
	gm.dataChangeDispatcher.addListener({
		onEvent: function(evt){
			// console.log("dataChangeDispatcher",evt)
			// Q.log(evt.kind + ' changed. from [' + evt.oldValue + '] to [' + evt.value + ']');
			if('name'==evt.kind) {
				var ele = evt.source
				var id = ele.get('id')
				if(id && self.isSender(ele)) {
					self.send('element_update_props' , {Id:ele.get('id'), Sender : self.uuid , Props:{name:evt.value}})
				}
				
			}
		}
	});
	// a.parent = b; 触发
	// gm.parentChangeDispatcher.addListener(function(evt){
	// 	Q.log(evt.source.name + '\'s ' + evt.kind + ' changed. from [' + evt.oldValue + '] to [' + evt.value.name + ']');
	// });
	//图元集合发生变化时（如添加，删除......）
	gm.listChangeDispatcher.addListener(function(evt){
		Q.log("listChangeDispatcher "+ evt.kind +" element");
		if('remove' ==evt.kind) { //删除元素
			var data = evt.data
			if(Array.isArray(data)){
				for(var i in evt.data) {
					var id = evt.data[i].get('id')
					if(!id || !(id in self.powerNet.elements)) {//元素不存在，不需要重复删除
						return
					}
					self.send("element_delete" , {Id: id})
				}
			}else {
				var id = evt.data.get('id')
				if(!id || !(id in self.powerNet.elements)) {//元素不存在，不需要重复删除
					return
				}
				self.send("element_delete" , {Id: data.get('id')})
			}
		}
		if('add' ==evt.kind) { //添加元素
			var node = evt.data
			var id =node.get('id')
			if(id && id in self.powerNet.elements) {//已经存在不需要重复创建
				return
			}
			// if(self.onElementAdd) {
			// 	self.onElementAdd(node , this)
			// }
			// if("Q.Edge" == node.type) { //添加了一条边
			// 	console.log(node.from ,node.to)
			// }
		}
	});
}
WsPowerNet.prototype.initWs = function() {
	// this.on("init" , this.onInit)
	this.on("graph_init" , this.onWsGraphInit)
	this.on("graph_update" , this.onWsGraphUpdate)
	this.on("element_new" , this.onWsElementNew)
	this.on("element_delete" , this.onWsElementDelete)
	this.on("element_update_props" , this.onWsNodeUpdateProps)
}
WsPowerNet.prototype.send = function(subject , data) {
	this.ws.send(JSON.stringify({Subject:subject , Data:data}))
}

WsPowerNet.prototype.on= function(subject, cb) {
	this.listeners[subject] = cb
}
WsPowerNet.prototype.handle= function(subject, data) {
	var cb = this.listeners[subject]
	if(cb) {
		cb(subject , data)
	}
}

WsPowerNet.prototype.sendInitGraph= function() {
	this.send("graph_init")
}
WsPowerNet.prototype.currentSubNetworkId = function() {
	var parent = this.powerNet.graph.currentSubNetwork
	if(parent) {
		return parent.get('id')
	}
	return null
}
//小demo
WsPowerNet.prototype.sendNewNode = function(protoId,x,y) {
	var n = {ProtoId : protoId*1 ,Props:{x:parseInt(x),y:parseInt(y)}}
	var parentId = this.currentSubNetworkId()
	if(parentId) {
		n.Props.parent = parentId
	}
	this.send("element_new" , n)
}
//小demo
WsPowerNet.prototype.sendNewEdge= function(protoId , from ,to) {
	var n = {ProtoId : protoId*1 ,Props:{from:from*1,to:to*1}}
	var parentId = this.currentSubNetworkId()
	if(parentId) {
		n.Props.parent = parentId
	}
	this.send("element_new" , n)
}
WsPowerNet.prototype.onWsGraphInit= function(subject, data) {
	console.log(subject,data)
	if(!data) {
		return
	}
	this.powerNet.clear()
	this.powerNet.loadElements(data.Elements)
}
WsPowerNet.prototype.onWsGraphUpdate= function(subject, data) {

}
WsPowerNet.prototype.onWsElementNew= function(subject, data) {
	this.powerNet.createElement(data)
}
WsPowerNet.prototype.onWsElementDelete= function(subject, data) {
	this.powerNet.removeElement(data.Id)
}
WsPowerNet.prototype.onWsNodeUpdateProps= function(subject, data) {
	var ele =this.powerNet.getElement(data.Id) 
	ele.set('sender' , data.Sender)
	this.powerNet.setProps(ele, data.Props)
}
WsPowerNet.prototype.isSender= function(ele) { //我是发出者吗？false：被动接受者
	var sender = ele.get('sender')
	return  !sender || sender == this.uuid
}


WsPowerNet.prototype.onGraphEndDrag= function(evt) {
	var ele = evt.getData()
	if(ele) {
		if(ele instanceof Q.Text) {
			this.send('element_update_props' , {Id:ele.get('id') , Props:{x:parseInt(ele.x) ,y:parseInt(ele.y) , styles:{"label.size":ele.styles['label.size']}}})
			return
		}
		if(ele instanceof Q.Node) {
			this.send('element_update_props' , {Id:ele.get('id') , Props:{x:parseInt(ele.x) ,y:parseInt(ele.y)}})
			return
		}
		
	}
}



WsPowerNet.prototype.createGroup= function(ids) { //我是发出者吗？false：被动接受者
	var sender = ele.get('sender')
	return  !sender || sender == this.uuid
}
WsPowerNet.prototype.removeGroup= function(ele) { //我是发出者吗？false：被动接受者
	var sender = ele.get('sender')
	return  !sender || sender == this.uuid
}